<template>
	<view class="sc">
		<headertop title="健康商城" str1="oridei" :back1="back1"></headertop>
		<image :src="_static('/images/Group 11694@2x.png')" class="scback" mode="widthFix"></image>
		<view class="" :style="{'margin-top':statusBarHeight+'px','width':'100%','height':barHeight+'px'}">
		</view>
		<view class="bottomcontent">
			<drag-button :isDock="true" :num="numgouwche" :existTabBar="true" @btnClick="btnClick" />
			<view class="leftmenu">
				<view :class="!category_id?'itembox activeys':'itembox'" @click="qiehuan('','leftmenu')">
					全部商品
				</view>
				<view :class="category_id==item.category_id?'itembox activeys':'itembox'"
					@click="qiehuan(item.category_id,'leftmenu')" v-for="(item,index) in fenleiarr" :key="index">
					{{item.name}}
				</view>
			</view>
			<view class="rightcontent">
				<view class="msgicon" v-if="!list.length">
					<image :src="_static('/images/wxnone.png')" mode="widthFix">
					</image>
					<view class="msgtext">
						暂无内容
					</view>
				</view>
				<view class="itemb" v-for="(item,index) in  list" :key="index" @click="gopage(item)">
					<image :src="item['image']" class="spimg" mode=""></image>
					<view class="texts">
						<view class="t1">
							{{item['title']}}
						</view>
						<view class="t2">
							{{item['subtitle']}}
						</view>
						<view class="t3">
							<view class="jiage">
								￥{{item['price'][0]}}
							</view>
							<!-- @click.stop="add('+',item)"  v-if="!item.num"-->
							<image :src="_static('/images/Group 11462@2x.png')" class="addicon" mode="">
							</image>
							<!-- 	<view class="gnq" v-if="item.num">
								<image @click.stop="add('-',item)" src="@/static/images/微信图片_20250716094829.png"
									class="addicon" mode=""></image>
								<span style="padding: 0 13rpx;">{{item.num}}</span>
								<image :src="_static('/images/Group 11462@2x.png')" @click.stop="add('+',item)"
									class="addicon" mode=""></image>
							</view> -->
						</view>

					</view>
				</view>
			</view>
		</view>
		<view class="" style="height: 200rpx;">

		</view>
		<Tabbar page="/pages/shoppingmal/shangcheng"></Tabbar>
		<userlogin ref="denglu"></userlogin>
	</view>
</template>

<script setup>
	import dragButton from "@/components/drag-button/drag-button.vue";
	import userlogin from '@/components/getuser/getuser.vue'
	import ulm, {
		$api
	} from '@/ulm';
	import Tabbar from '@/components/tabbar.vue'
	import headertop from '@/components/header.vue'
	import {
		_static
	} from '@/ulm/utils/url.js'
	import {
		ref,
		reactive,
		onMounted
	} from 'vue'
	import {
		onPageScroll,
		onLoad,
		onShow,
		onReachBottom
	} from "@dcloudio/uni-app";
	import {
		setToken,
		getToken,
	} from '@/ulm/utils/auth';
	const category_id = ref('')
	const statusBarHeight = ref('')
	const texts = ref('')
	const barHeight = ref('')
	const list = ref([])
	const fenleiarr = ref([])
	const total = ref(0)
	const numgouwche = ref(0)
	const back1 = ref('')
	const denglu = ref(null)
	const active = ref(2)
	const formData = reactive({
		category_id: '',
		page: 1,
		list_rows: 10
	})
	onShow(() => {
		list.value = []
		formData['page'] = 1
		category_id.value = ''
		init()
	})

	onReachBottom(() => {
		if (total.value > list.value.length) {
			formData['page']++
			qiehuan(category_id.value)
		}
	})
	onMounted(() => {
		statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
		const {
			top,
			height
		} = wx.getMenuButtonBoundingClientRect();
		barHeight.value = height ? height + (top - statusBarHeight.value) * 2 : 38;
	})
	onPageScroll((e) => {
		if (e.scrollTop > 100) {
			back1.value = '#81D6FF'
		} else {
			back1.value = ''
		}
	})

	async function carnuminit() {
		const res = await $api.cartnum()
		if (res.code == 1) {
			for (var i in list.value) {
				var ind = res.data.findIndex(item => item.sku_price['id'] == list.value[i]['id'])
				if (ind == -1) {
					list.value[i]['num'] = 0
				} else {
					list.value[i]['num'] = res.data[ind]['goods_num']
				}
			}
			var arr1 = res.data
			var jiage = arr1.reduce((e, item) => {
				return e + item.goods_num
			}, 0)
			numgouwche.value = jiage
		}
	}
	async function add(type, item) {
		if (!getToken()) {
			denglu.value.DengLu()
			return
		}
		var obj = {
			goods_id: item.id,
			goods_num: 1,
			goods_sku_price_id: item.new_sku_prices[0]['id'],
			type: type == '+' ? '' : 'dec'
		}
		const res = await $api.cartupdate(obj)
		if (res.code == 1) {
			carnuminit()
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}

	function btnClick() {
		uni.navigateTo({
			url: '/sub_shoppingmal/index/gouwuche'
		})
	}

	function gopage(item) {
		uni.navigateTo({
			url: '/sub_shoppingmal/index/detail?id=' + item.id
		})
	}
	async function init() {
		const res = await $api.shoprocategory()
		if (res.code == 1) {
			fenleiarr.value = res.data
			qiehuan()
		}
	}
	async function qiehuan(id, str) {
		if (id == category_id.value) return
		category_id.value = id
		formData.category_id = id
		if (str == 'leftmenu') {
			list.value = []
			formData.page = 1
		}
		const res = await $api.shoprogoods(formData)
		if (res.code == 1) {
			list.value.push(...res.data.data)
			total.value = res.data.total
			carnuminit()
		}
	}
</script>
<style lang="scss">
	.msgicon {
		width: 408rpx;
		margin: 100rpx auto;
		overflow: hidden;

		.msgtext {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #86909C;
			text-align: center;
			width: 100%;
			margin-top: 30rpx;
		}

		image {
			width: 408rpx;
		}
	}

	.sc {
		width: 100%;
		overflow: hidden;
		position: relative;

		.scback {
			width: 100%;
			z-index: -1;
			position: absolute;
		}

		.bottomcontent {
			width: 100%;
			margin-top: 328rpx;
			overflow: hidden;
			background: #FFFFFF;
			box-shadow: 0rpx -4rpx 8rpx 0rpx rgba(171, 227, 254, 0.31);
			border-radius: 48rpx 48rpx 0rpx 0rpx;
			height: 1134rpx;
			display: flex;

			.rightcontent {
				width: 452rpx;
				height: 1134rpx;
				margin-left: 28rpx;
				overflow: hidden;

				.itemb {
					width: 100%;
					height: 176rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 2rpx solid #F2F3F5;

					.spimg {
						width: 128rpx;
						height: 128rpx;
						border-radius: 12rpx 12rpx 12rpx 12rpx;
					}

					.texts {
						width: 308rpx;
						overflow: hidden;

						.t1 {
							width: 100%;
							white-space: nowrap; // 防止文本换行
							overflow: hidden; // 隐藏溢出的文本
							text-overflow: ellipsis; // 显示省略号
							font-size: 28rpx;
							color: #1D2129;
						}

						.t2 {
							width: 100%;
							white-space: nowrap; // 防止文本换行
							overflow: hidden; // 隐藏溢出的文本
							text-overflow: ellipsis; // 显示省略号
							margin-top: 8rpx;
							font-weight: 400;
							font-size: 24rpx;
							color: #4E5969;
						}

						.t3 {
							width: 100%;
							display: flex;
							align-items: center;
							justify-content: space-between;
							margin-top: 30rpx;

							.gnq {
								display: flex;
								align-items: center;
								font-family: PingFang SC, PingFang SC;
								font-weight: 500;
								font-size: 30rpx;
								color: #4E5969;

								.addicon {
									width: 32rpx;
									height: 32rpx;
								}
							}

							.jiage {
								font-family: PingFang SC, PingFang SC;
								font-weight: bold;
								font-size: 28rpx;
								color: #F53F3F;
							}

							.addicon {
								width: 32rpx;
								height: 32rpx;
							}
						}

					}
				}
			}

			.leftmenu {
				height: 1134rpx;
				width: 238rpx;
				border-right: 2rpx solid #E5E6EB;
				overflow: hidden;
				background: #F7F8FA;

				.itembox {
					width: 238rpx;
					height: 96rpx;
					text-align: center;
					line-height: 96rpx;
					font-size: 28rpx;
					font-weight: 400;
					border-radius: 0rpx 0rpx 0rpx 0rpx;
				}

				.activeys {
					background: #2AB4F5;
					font-weight: bold;
					color: #FFFFFF;
				}
			}
		}
	}
</style>